package menu_api

import (
	"ginBolg/global"
	"ginBolg/models"
	"ginBolg/models/res"
	"github.com/gin-gonic/gin"
)

type Banner struct {
	ID   uint   `json:"id"`
	Path string `json:"path"`
}

type MenuResponse struct {
	MenuModel models.MenuModel
	Banners   []Banner `json:"banners"`
}

func (MenuApi) MenuListView(c *gin.Context) {
	// 先查菜单
	var menuList []models.MenuModel
	var menuIDList []uint
	global.DB.Find(&menuList).Select("id").Scan(menuIDList)
	// 查关联表
	var menuBanners models.MenuBannerModel
	global.DB.Order("sort desc").Find(&menuBanners, "menu_id in ?", menuIDList)
	var menus []MenuResponse
	for _, menu := range menuList {
		var banners = make([]Banner, 0)
		for _, banner := range menu.Banners {
			if banner.ID != menu.ID {
				continue
			}
			banners = append(banners, Banner{
				ID:   banner.ID,
				Path: banner.Path,
			})
		}
		menus = append(menus, MenuResponse{
			MenuModel: menu,
			Banners:   banners,
		})
	}
	res.OkWithData(menus, c)
	return
}
